// pages/flight/add_tickets.js
const app = getApp();

let req = require('../../utils/request.js');
let api = require('../../utils/api.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    type_arr: ['经济舱', '头等 / 商务舱'],
    type_text: '选择机票舱位',
    index: 0,
    checked: {
      checked_0: 'none',
      checked_1: 'none',
      checked_2: 'none',
      checked_3: 'none'
    },
    optional: {
      value_0: '',
      value_1: '',
      value_2: '',
      value_3: ''
    },
    checked_switch: {
      switch_0: '',
      switch_1: '',
      switch_2: '',
      switch_3: '',
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中'
    })
    var flt_id = options.flt_id,
        tic_id = options.tic_id,
        _this = this,
        data = {}
    
    if(flt_id) {
      wx.hideLoading();
      data['flt_id'] = flt_id
    }else if(tic_id) {
      data['tic_id'] = tic_id
      req.request(
        api.getTicketInfoById,
        {method: 'POST', data: {id: tic_id}},
        function(res) {
          wx.hideLoading();
          res = res.data

          if(res.status != 1) {
            wx.showToast({
              title: res.msg,
              icon: 'none'
            })

            setTimeout(function() {
              wx.navigateBack({
                delta: 1
              })
            }, 2000)
          }else {
            var info = res.info,
                data = {}

            if(info.tic_discount != '0.00') {
              data['checked_switch.switch_0'] = 'checked'
              data['optional.value_0'] = info.tic_discount
              data['checked.checked_0'] = 'block'
            }
            if(info.tic_price_child != '0.00') {
              data['checked_switch.switch_1'] = 'checked'
              data['optional.value_1'] = info.tic_price_child
              data['checked.checked_1'] = 'block'
            }
            if (info.tic_price_baby != '0.00') {
              data['checked_switch.switch_2'] = 'checked'
              data['optional.value_2'] = info.tic_price_baby
              data['checked.checked_2'] = 'block'
            }
            if (info.tic_back != '0.00') {
              data['checked_switch.switch_3'] = 'checked'
              data['optional.value_3'] = info.tic_back
              data['checked.checked_3'] = 'block'
            }
            data['info'] = info
            data['index'] = info.tic_type
            _this.setData(data)
          }
        }
      )
    }

    _this.setData(data)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  selectType(e) {
    var index = e.detail.value,
        _this = this,
        type_arr = _this.data.type_arr
    
    _this.setData({ index: index, color: 'checked', type_text: type_arr[index] })
  },
  checkStatus(e) {
    var index = e.currentTarget.dataset.index,
        checked = e.detail.value,
        _this = this,
        data = {}

    if (checked) {
      data['checked.checked_' + index] = 'block'
    }else {
      data['checked.checked_' + index] = 'none'
      data['optional.value_' + index] = ''
    }
    _this.setData(data)
  },
  submit(e) {
    wx.showLoading({
      title: '加载中',
    })
    var value = e.detail.value,
        _this = this,
        flt_id = _this.data.flt_id,
        tic_id = _this.data.tic_id,
        url = ''

    if(flt_id) {
      value['parent_flt_id'] = flt_id
      url = api.addTickets
    }else if(tic_id) {
      value['tic_id'] = tic_id  
      url = api.addTickets
    }

    req.request(
      url,
      { method: 'POST', data: value },
      function (res) {
        wx.hideLoading();
        res = res.data

        wx.showToast({
          title: res.msg,
          icon: 'none'
        })

        if(res.status == 1) {
          setTimeout(function() {
            wx.navigateBack({
              delta: 1
            })
          }, 2000)
        }
      }
    )
  }
})